import { createSlice } from "@reduxjs/toolkit";
import { RootState } from "../index.ts";

interface UserState {
  users: IUserType[];
  roles: IRole[];
  crtUser: ICrtUser;
}

export interface ICrtUser {
  id: string;
}

export interface IUserType {
  id: string;
  nike: string;
  gender: number;
  role: number;
  phoneNumber: string;
  createTime: string;
}

export interface IRole {
  value: number;
  label: string;
  color: string;
  createTime: string;
}

const initialState: UserState = {
  users: [
    {
      id: "1",
      nike: "王芳芳",
      gender: 1,
      role: 0,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "2",
      nike: "李雷",
      gender: 0,
      role: 1,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "3",
      nike: "陈志华",
      gender: 0,
      role: 1,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "4",
      nike: "安千萍",
      gender: 1,
      role: 3,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "5",
      nike: "化霜",
      gender: 1,
      role: 2,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "6",
      nike: "潜龙",
      gender: 0,
      role: 2,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "7",
      nike: "汤倩",
      gender: 1,
      role: 1,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "8",
      nike: "林俊杰",
      gender: 0,
      role: 3,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "9",
      nike: "杨丞琳",
      gender: 1,
      role: 2,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "10",
      nike: "特朗普",
      gender: 0,
      role: 1,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
    {
      id: "11",
      nike: "小特朗普",
      gender: 0,
      role: 1,
      phoneNumber: "12345678",
      createTime: "2021-03-01",
    },
  ],
  roles: [
    { label: "仓库管理员", color: "blue", value: 0, createTime: "2025-03-01" },
    { label: "管理员", color: "red", value: 1, createTime: "2025-03-01" },
    { label: "员工", color: "green", value: 2, createTime: "2025-03-01" },
    { label: "店长", color: "purple", value: 3, createTime: "2025-03-01" },
  ],
  crtUser: {
    id: "1",
  },
};

const usersSlice = createSlice({
  name: "users",
  initialState,
  reducers: {
    addUser(state, action) {
      state.users.push(action.payload);
    },
    updateUser(state, action) {
      const { id, nike, gender, role, phoneNumber } = action.payload;
      const user = state.users.find((user) => user.id === id);

      if (user) {
        user.nike = nike;
        user.gender = gender;
        user.role = role;
        user.phoneNumber = phoneNumber;
      }
    },
    addRole(state, action) {
      state.roles.push(action.payload);
    },
    updateRole(state, action) {
      const { value, label, color } = action.payload;

      const role = state.roles.find((role) => role.value === value);

      if (role) {
        role.label = label;
        role.color = color;
      }
    },
    deleteRole(state, action) {
      const { value } = action.payload;
      state.roles = state.roles.filter((role) => role.value !== value);
    },
  },
});

export default usersSlice.reducer;

export const { addUser, updateUser, updateRole, deleteRole, addRole } =
  usersSlice.actions;

export const selectUserById = (state: RootState, id: string) =>
  state.users.users.find((user: IUserType) => user.id === id);

export const selectAllRoles = (state: RootState) => state.users.roles;
